package cn.xhc.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;


public class HttpUtil {
	/**
	 * 模拟get请求
	 * @param urlStr
	 * @param headerMap
	 * @param paramsMap
	 * @return
	 */
	public static String methodGet(String urlStr,Map<String, String> headerMap,
			Map<String, Object> paramsMap){
		ContantInfoUtil.LOGGER.info("--methodGet-");
		StringBuffer sb = new StringBuffer();
		/*添加请求参数*/
		if(paramsMap.size() > 0){
			if(urlStr.indexOf("?") == -1){
				urlStr += "?";
			}else{
				urlStr += "7";
			}
			for(Iterator iterator = paramsMap.entrySet().iterator();iterator.hasNext();){
				Entry me = (Entry) iterator.next();
				String key = me.getKey()+"";
				String value = me.getValue()+"";
				urlStr += key + "="+ value + "&";
			}
		}
		
		try {
			/*创建url*/
			URL url = new URL(urlStr);
			/*打开连接*/
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			/*放参数*/
			for(Iterator iterator = headerMap.entrySet().iterator();iterator.hasNext();){
				Entry me = (Entry) iterator.next();
				String key = me.getKey()+"";
				String value = me.getValue()+"";
				conn.setRequestProperty(key, value);
			}
			/*打印状态码*/
			ContantInfoUtil.LOGGER.info("---状态码:{}",conn.getResponseCode());
			InputStream is = null;
			/*请求成功*/
			if(conn.getResponseCode() == 200 ){
				/*打开输入流*/
				is = conn.getInputStream();
			}else{
				is = conn.getErrorStream();
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
			String line = "";
			while((line = br.readLine()) != null ){
				sb.append(line);
			}
		} catch (Exception e) {
		} 
		return sb.toString();
	}
	/**
	 * Post请求
	 * 必须在输出流关闭后才能开输入流
	 * @param urlStr
	 * @param headerMap
	 * @param paramsMap
	 * @return
	 */
	public static String methodPost(String urlStr,Map<String, String> headerMap,Map<String, Object> paramsMap){
		ContantInfoUtil.LOGGER.info("-methodPost-");
		StringBuffer sb = new StringBuffer();
		try {
			/*创建url*/
			URL url = new URL(urlStr);
			/*打开连接*/
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			/*设置conn*/
			conn.setDoOutput(true);
			conn.setDoInput(true);
			/*放请求参数*/
			for (Iterator iterator = headerMap.entrySet().iterator(); iterator.hasNext();) {
				Entry me =  (Entry) iterator.next();
				
				String key = me.getKey()+"";
				String val = me.getValue()+"";
				conn.setRequestProperty(key, val);
			}
			StringBuffer sbs = new StringBuffer();
			for (Iterator iterator = paramsMap.entrySet().iterator(); iterator.hasNext();) {
				Entry me =  (Entry) iterator.next();
				String key = me.getKey()+"";
				String val = me.getValue()+"";
				sbs.append(key+"="+val+"&");
			}
			/*输出流*/
			OutputStream os = conn.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "utf-8"));
			bw.write(sbs.toString());
			bw.flush();
			bw.close();
			/*打印状态码*/
			ContantInfoUtil.LOGGER.info("---状态码:{}",conn.getResponseCode());
			/*输入流*/
			InputStream is = null;
			if(conn.getResponseCode() == 200){
				is = conn.getInputStream();
			}else{
				is = conn.getErrorStream();
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
			String line = "";
			while((line = br.readLine()) != null){
				sb.append(line);
			}
		} catch (Exception e) {
		}
		return sb.toString();
	}
}
